package com.apache.ibatis.configuration;

import com.alibaba.druid.pool.DruidDataSource;
import com.apache.ibatis.io.Resources;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.List;
import java.util.Properties;

public class XmlConfigBuilder {

    private Configuration configuration;


    public XmlConfigBuilder() {
    }

    public void parseMybatisConfig(InputStream inputStream) {
        try {
            SAXReader reader = new SAXReader();
            Document document = reader.read(inputStream);
            Element rootElement = document.getRootElement();
            List<Node> nodePropertyList = rootElement.selectNodes("//property");
            Properties properties = new Properties();
            if (nodePropertyList != null && (!nodePropertyList.isEmpty())) {
                for (Node node : nodePropertyList) {
                    if (node instanceof Element) {
                        Element element = (Element) node;
                        String name = element.attributeValue("name");
                        String value = element.attributeValue("value");
                        properties.setProperty(name,value);
                    }
                }

                DruidDataSource dataSource = new DruidDataSource();
                dataSource.setDriverClassName(properties.getProperty("driver"));
                dataSource.setUrl(properties.getProperty("url"));
                dataSource.setUsername(properties.getProperty("username"));
                dataSource.setPassword(properties.getProperty("password"));

                // 连接池配置
                dataSource.setInitialSize(5);
                dataSource.setMinIdle(5);
                dataSource.setMaxActive(20);
                dataSource.setMaxWait(60000);

                // 连接有效性检测配置
                dataSource.setValidationQuery("SELECT 1");
                dataSource.setTestWhileIdle(true);
                dataSource.setTestOnBorrow(false);
                dataSource.setTestOnReturn(false);
                dataSource.setTimeBetweenEvictionRunsMillis(60000);
                dataSource.setMinEvictableIdleTimeMillis(300000);

                // 配置过滤器（开启Web和SQL监控、防御SQL注入）
                dataSource.setFilters("stat,wall");

                configuration.setDataSource(dataSource);
            }

            List<Node> nodeMapperList = rootElement.selectNodes("//mapper");
            if (nodeMapperList != null && (!nodeMapperList.isEmpty())) {
                for (Node node : nodeMapperList) {
                    if (node instanceof Element) {
                        Element element = (Element) node;
                        String resources = element.attributeValue("resources");
                        InputStream resourcesInputStream = Resources.getResourcesAsStream(resources);
                        XmlMapperBuilder xmlMapperBuilder = new XmlMapperBuilder(configuration);
                        xmlMapperBuilder.parse(resourcesInputStream);
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
